home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / GNU / emacs.inst / emacs19.idb / usr / gnu / info / elisp-5.z / elisp-5
Encoding:
GNU Info File  |  1994-08-02  |  49.1 KB  |  1,335 lines

  1. This is Info file elisp, produced by Makeinfo-1.55 from the input file
  2. elisp.texi.
  3.  
  4.    This version is newer than the second printed edition of the GNU
  5. Emacs Lisp Reference Manual.  It corresponds to Emacs Version 19.19.
  6.  
  7.    Published by the Free Software Foundation 675 Massachusetts Avenue
  8. Cambridge, MA 02139 USA
  9.  
  10.    Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  11.  
  12.    Permission is granted to make and distribute verbatim copies of this
  13. manual provided the copyright notice and this permission notice are
  14. preserved on all copies.
  15.  
  16.    Permission is granted to copy and distribute modified versions of
  17. this manual under the conditions for verbatim copying, provided that
  18. the entire resulting derived work is distributed under the terms of a
  19. permission notice identical to this one.
  20.  
  21.    Permission is granted to copy and distribute translations of this
  22. manual into another language, under the above conditions for modified
  23. versions, except that this permission notice may be stated in a
  24. translation approved by the Foundation.
  25.  
  26.    Permission is granted to copy and distribute modified versions of
  27. this manual under the conditions for verbatim copying, provided also
  28. that the section entitled "GNU Emacs General Public License" is included
  29. exactly as in the original, and provided that the entire resulting
  30. derived work is distributed under the terms of a permission notice
  31. identical to this one.
  32.  
  33.    Permission is granted to copy and distribute translations of this
  34. manual into another language, under the above conditions for modified
  35. versions, except that the section entitled "GNU Emacs General Public
  36. License" may be included in a translation approved by the Free Software
  37. Foundation instead of in the original English.
  38.  
  39. 
  40. File: elisp,  Node: Case Table,  Prev: Character Case,  Up: Strings and Characters
  41.  
  42. The Case Table
  43. ==============
  44.  
  45.    You can customize case conversion by installing a special "case
  46. table".  A case table specifies the mapping between upper case and lower
  47. case letters.  It affects both the string and character case conversion
  48. functions (see the previous section) and those that apply to text in the
  49. buffer (*note Case Changes::.).  Use case table if you are using a
  50. language which has letters that are not the standard ASCII letters.
  51.  
  52.    A case table is a list of this form:
  53.  
  54.      (DOWNCASE UPCASE CANONICALIZE EQUIVALENCES)
  55.  
  56. where each element is either `nil' or a string of length 256.  The
  57. element DOWNCASE says how to map each character to its lower-case
  58. equivalent.  The element UPCASE maps each character to its upper-case
  59. equivalent.  If lower and upper case characters are in one-to-one
  60. correspondence, use `nil' for UPCASE; then Emacs deduces the upcase
  61. table from DOWNCASE.
  62.  
  63.    For some languages, upper and lower case letters are not in
  64. one-to-one correspondence.  There may be two different lower case
  65. letters with the same upper case equivalent.  In these cases, you need
  66. to specify the maps for both directions.
  67.  
  68.    The element CANONICALIZE maps each character to a canonical
  69. equivalent; any two characters that are related by case-conversion have
  70. the same canonical equivalent character.
  71.  
  72.    The element EQUIVALENCES is a map that cyclicly permutes each
  73. equivalence class (of characters with the same canonical equivalent).
  74. (For ordinary ASCII, this would map `a' into `A' and `A' into `a', and
  75. likewise for each set of equivalent characters.)
  76.  
  77.    You can provide `nil' for both CANONICALIZE and EQUIVALENCES, in
  78. which case both are deduced from DOWNCASE and UPCASE.  Normally, that's
  79. what you should do, when you construct a case table.  Alternatively,
  80. you can provide suitable strings for both CANONICALIZE and
  81. EQUIVALENCES.  When you look at the case table that's in use, you will
  82. find non-`nil' values for those components.  Do not try to make just
  83. one of these components `nil'; that is not meaningful.
  84.  
  85.    Each buffer has a case table.  Emacs also has a "standard case
  86. table" which is copied into each buffer when you create the buffer.
  87. (Changing the standard case table doesn't affect any existing buffers.)
  88.  
  89.    Here are the functions for working with case tables:
  90.  
  91.  - Function: case-table-p OBJECT
  92.      This predicate returns non-`nil' if OBJECT is a valid case table.
  93.  
  94.  - Function: set-standard-case-table TABLE
  95.      This function makes TABLE the standard case table, so that it will
  96.      apply to any buffers created subsequently.
  97.  
  98.  - Function: standard-case-table
  99.      This returns the standard case table.
  100.  
  101.  - Function: current-case-table
  102.      This function returns the current buffer's case table.
  103.  
  104.  - Function: set-case-table TABLE
  105.      This sets the current buffer's case table to TABLE.
  106.  
  107.    The following three functions are convenient subroutines for packages
  108. that define non-ASCII character sets.  They modify a string
  109. DOWNCASE-TABLE provided as an argument; this should be a string to be
  110. used as the DOWNCASE part of a case table.  They also modify two syntax
  111. tables, the standard syntax table and the Text mode syntax table.
  112. (*Note Syntax Tables::.)
  113.  
  114.  - Function: set-case-syntax-pair UC LC DOWNCASE-TABLE
  115.      This function specifies a pair of corresponding letters, one upper
  116.      case and one lower case.
  117.  
  118.  - Function: set-case-syntax-delims L R DOWNCASE-TABLE
  119.      This function makes characters L and R a matching pair of
  120.      case-invariant delimiters.
  121.  
  122.  - Function: set-case-syntax CHAR SYNTAX DOWNCASE-TABLE
  123.      This function makes CHAR case-invariant, with syntax SYNTAX.
  124.  
  125.  - Command: describe-buffer-case-table
  126.      This command displays a description of the contents of the current
  127.      buffer's case table.
  128.  
  129.    You can load the library `iso-syntax' to set up the syntax and case
  130. table for the 256 bit ISO Latin 1 character set.
  131.  
  132. 
  133. File: elisp,  Node: Lists,  Next: Sequences Arrays Vectors,  Prev: Strings and Characters,  Up: Top
  134.  
  135. Lists
  136. *****
  137.  
  138.    A "list" represents a sequence of zero or more elements (which may
  139. be any Lisp objects).  The important difference between lists and
  140. vectors is that two or more lists can share part of their structure; in
  141. addition, you can insert or delete elements in a list without copying
  142. the whole list.
  143.  
  144. * Menu:
  145.  
  146. * Cons Cells::          How lists are made out of cons cells.
  147. * Lists as Boxes::                 Graphical notation to explain lists.
  148. * List-related Predicates::        Is this object a list?  Comparing two lists.
  149. * List Elements::       Extracting the pieces of a list.
  150. * Building Lists::      Creating list structure.
  151. * Modifying Lists::     Storing new pieces into an existing list.
  152. * Sets And Lists::      A list can represent a finite mathematical set.
  153. * Association Lists::   A list can represent a finite relation or mapping.
  154.  
  155. 
  156. File: elisp,  Node: Cons Cells,  Next: Lists as Boxes,  Prev: Lists,  Up: Lists
  157.  
  158. Lists and Cons Cells
  159. ====================
  160.  
  161.    Lists in Lisp are not a primitive data type; they are built up from
  162. "cons cells".  A cons cell is a data object which represents an ordered
  163. pair.  It records two Lisp objects, one labeled as the CAR, and the
  164. other labeled as the CDR.  (These names are traditional.)
  165.  
  166.    A list is made by chaining cons cells together, one cons cell per
  167. element.  By convention, the CARs of the cons cells are the elements of
  168. the list, and the CDRs are used to chain the list: the CDR of each cons
  169. cell is the following cons cell.  The CDR of the last cons cell is
  170. `nil'.  This asymmetry between the CAR and the CDR is entirely a matter
  171. of convention; at the level of cons cells, the CAR and CDR slots have
  172. the same characteristics.
  173.  
  174.    The symbol `nil' is considered a list as well as a symbol; it is the
  175. list with no elements.  For convenience, the symbol `nil' is considered
  176. to have `nil' as its CDR (and also as its CAR).
  177.  
  178.    The CDR of any nonempty list L is a list containing all the elements
  179. of L except the first.
  180.  
  181. 
  182. File: elisp,  Node: Lists as Boxes,  Next: List-related Predicates,  Prev: Cons Cells,  Up: Lists
  183.  
  184. Lists as Linked Pairs of Boxes
  185. ==============================
  186.  
  187.    A cons cell can be illustrated as a pair of boxes.  The first box
  188. represents the CAR and the second box represents the CDR.  Here is an
  189. illustration of the two-element list, `(tulip lily)', made from two
  190. cons cells:
  191.  
  192.       ---------------         ---------------
  193.      | car   | cdr   |       | car   | cdr   |
  194.      | tulip |   o---------->| lily  |  nil  |
  195.      |       |       |       |       |       |
  196.       ---------------         ---------------
  197.  
  198.    Each pair of boxes represents a cons cell.  Each box "refers to",
  199. "points to" or "contains" a Lisp object.  (These terms are synonymous.)
  200. The first box, which is the CAR of the first cons cell, contains the
  201. symbol `tulip'.  The arrow from the CDR of the first cons cell to the
  202. second cons cell indicates that the CDR of the first cons cell points
  203. to the second cons cell.
  204.  
  205.    The same list can be illustrated in a different sort of box notation
  206. like this:
  207.  
  208.          ___ ___      ___ ___
  209.         |___|___|--> |___|___|--> nil
  210.           |            |
  211.           |            |
  212.            --> tulip    --> lily
  213.  
  214.    Here is a more complex illustration, this time of the three-element
  215. list, `((pine needles) oak maple)', the first element of which is a
  216. two-element list:
  217.  
  218.          ___ ___      ___ ___      ___ ___
  219.         |___|___|--> |___|___|--> |___|___|--> nil
  220.           |            |            |
  221.           |            |            |
  222.           |             --> oak      --> maple
  223.           |
  224.           |     ___ ___      ___ ___
  225.            --> |___|___|--> |___|___|--> nil
  226.                  |            |
  227.                  |            |
  228.                   --> pine     --> needles
  229.  
  230.    The same list is represented in the first box notation like this:
  231.  
  232.       --------------       --------------       --------------
  233.      | car   | cdr  |     | car   | cdr  |     | car   | cdr  |
  234.      |   o   |   o------->| oak   |   o------->| maple |  nil |
  235.      |   |   |      |     |       |      |     |       |      |
  236.       -- | ---------       --------------       --------------
  237.          |
  238.          |
  239.          |        --------------       ----------------
  240.          |       | car   | cdr  |     | car     | cdr  |
  241.           ------>| pine  |   o------->| needles |  nil |
  242.                  |       |      |     |         |      |
  243.                   --------------       ----------------
  244.  
  245.    *Note List Type::, for the read and print syntax of lists, and for
  246. more "box and arrow" illustrations of lists.
  247.  
  248. 
  249. File: elisp,  Node: List-related Predicates,  Next: List Elements,  Prev: Lists as Boxes,  Up: Lists
  250.  
  251. Predicates on Lists
  252. ===================
  253.  
  254.    The following predicates test whether a Lisp object is an atom, is a
  255. cons cell or is a list, or whether it is the distinguished object `nil'.
  256. (Many of these tests can be defined in terms of the others, but they are
  257. used so often that it is worth having all of them.)
  258.  
  259.  - Function: consp OBJECT
  260.      This function returns `t' if OBJECT is a cons cell, `nil'
  261.      otherwise.  `nil' is not a cons cell, although it *is* a list.
  262.  
  263.  - Function: atom OBJECT
  264.      This function returns `t' if OBJECT is an atom, `nil' otherwise.
  265.      All objects except cons cells are atoms.  The symbol `nil' is an
  266.      atom and is also a list; it is the only Lisp object which is both.
  267.  
  268.           (atom OBJECT) == (not (consp OBJECT))
  269.  
  270.  - Function: listp OBJECT
  271.      This function returns `t' if OBJECT is a cons cell or `nil'.
  272.      Otherwise, it returns `nil'.
  273.  
  274.           (listp '(1))
  275.                => t
  276.           (listp '())
  277.                => t
  278.  
  279.  - Function: nlistp OBJECT
  280.      This function is the opposite of `listp': it returns `t' if OBJECT
  281.      is not a list.  Otherwise, it returns `nil'.
  282.  
  283.           (listp OBJECT) == (not (nlistp OBJECT))
  284.  
  285.  - Function: null OBJECT
  286.      This function returns `t' if OBJECT is `nil', and returns `nil'
  287.      otherwise.  This function is identical to `not', but as a matter
  288.      of clarity we use `null' when OBJECT is considered a list and
  289.      `not' when it is considered a truth value (see `not' in *Note
  290.      Combining Conditions::).
  291.  
  292.           (null '(1))
  293.                => nil
  294.           (null '())
  295.                => t
  296.  
  297. 
  298. File: elisp,  Node: List Elements,  Next: Building Lists,  Prev: List-related Predicates,  Up: Lists
  299.  
  300. Accessing Elements of Lists
  301. ===========================
  302.  
  303.  - Function: car CONS-CELL
  304.      This function returns the value pointed to by the first pointer of
  305.      the cons cell CONS-CELL.  Expressed another way, this function
  306.      returns the CAR of CONS-CELL.
  307.  
  308.      As a special case, if CONS-CELL is `nil', then `car' is defined to
  309.      return `nil'; therefore, any list is a valid argument for `car'.
  310.      An error is signaled if the argument is not a cons cell or `nil'.
  311.  
  312.           (car '(a b c))
  313.                => a
  314.           (car '())
  315.                => nil
  316.  
  317.  - Function: cdr CONS-CELL
  318.      This function returns the value pointed to by the second pointer of
  319.      the cons cell CONS-CELL.  Expressed another way, this function
  320.      returns the CDR of CONS-CELL.
  321.  
  322.      As a special case, if CONS-CELL is `nil', then `cdr' is defined to
  323.      return `nil'; therefore, any list is a valid argument for `cdr'.
  324.      An error is signaled if the argument is not a cons cell or `nil'.
  325.  
  326.           (cdr '(a b c))
  327.                => (b c)
  328.           (cdr '())
  329.                => nil
  330.  
  331.  - Function: car-safe OBJECT
  332.      This function lets you take the CAR of a cons cell while avoiding
  333.      errors for other data types.  It returns the CAR of OBJECT if
  334.      OBJECT is a cons cell, `nil' otherwise.  This is in contrast to
  335.      `car', which signals an error if OBJECT is not a list.
  336.  
  337.           (car-safe OBJECT)
  338.           ==
  339.           (let ((x OBJECT))
  340.             (if (consp x)
  341.                 (car x)
  342.               nil))
  343.  
  344.  - Function: cdr-safe OBJECT
  345.      This function lets you take the CDR of a cons cell while avoiding
  346.      errors for other data types.  It returns the CDR of OBJECT if
  347.      OBJECT is a cons cell, `nil' otherwise.  This is in contrast to
  348.      `cdr', which signals an error if OBJECT is not a list.
  349.  
  350.           (cdr-safe OBJECT)
  351.           ==
  352.           (let ((x OBJECT))
  353.             (if (consp x)
  354.                 (cdr x)
  355.               nil))
  356.  
  357.  - Function: nth N LIST
  358.      This function returns the Nth element of LIST.  Elements are
  359.      numbered starting with zero, so the CAR of LIST is element number
  360.      zero.  If the length of LIST is N or less, the value is `nil'.
  361.  
  362.      If N is less than zero, then the first element is returned.
  363.  
  364.           (nth 2 '(1 2 3 4))
  365.                => 3
  366.           (nth 10 '(1 2 3 4))
  367.                => nil
  368.           (nth -3 '(1 2 3 4))
  369.                => 1
  370.           
  371.           (nth n x) == (car (nthcdr n x))
  372.  
  373.  - Function: nthcdr N LIST
  374.      This function returns the Nth cdr of LIST.  In other words, it
  375.      removes the first N links of LIST and returns what follows.
  376.  
  377.      If N is less than or equal to zero, then all of LIST is returned.
  378.      If the length of LIST is N or less, the value is `nil'.
  379.  
  380.           (nthcdr 1 '(1 2 3 4))
  381.                => (2 3 4)
  382.           (nthcdr 10 '(1 2 3 4))
  383.                => nil
  384.           (nthcdr -3 '(1 2 3 4))
  385.                => (1 2 3 4)
  386.  
  387. 
  388. File: elisp,  Node: Building Lists,  Next: Modifying Lists,  Prev: List Elements,  Up: Lists
  389.  
  390. Building Cons Cells and Lists
  391. =============================
  392.  
  393.    Many functions build lists, as lists reside at the very heart of
  394. Lisp.  `cons' is the fundamental list-building function; however, it is
  395. interesting to note that `list' is used more times in the source code
  396. for Emacs than `cons'.
  397.  
  398.  - Function: cons OBJECT1 OBJECT2
  399.      This function is the fundamental function used to build new list
  400.      structure.  It creates a new cons cell, making OBJECT1 the CAR,
  401.      and OBJECT2 the CDR.  It then returns the new cons cell.  The
  402.      arguments OBJECT1 and OBJECT2 may be any Lisp objects, but most
  403.      often OBJECT2 is a list.
  404.  
  405.           (cons 1 '(2))
  406.                => (1 2)
  407.           (cons 1 '())
  408.                => (1)
  409.           (cons 1 2)
  410.                => (1 . 2)
  411.  
  412.      `cons' is often used to add a single element to the front of a
  413.      list.  This is called "consing the element onto the list".  For
  414.      example:
  415.  
  416.           (setq list (cons newelt list))
  417.  
  418.      Note that there is no conflict between the variable named `list'
  419.      used in this example and the function named `list' described below;
  420.      any symbol can serve both functions.
  421.  
  422.  - Function: list &rest OBJECTS
  423.      This function creates a list with OBJECTS as its elements.  The
  424.      resulting list is always `nil'-terminated.  If no OBJECTS are
  425.      given, the empty list is returned.
  426.  
  427.           (list 1 2 3 4 5)
  428.                => (1 2 3 4 5)
  429.           (list 1 2 '(3 4 5) 'foo)
  430.                => (1 2 (3 4 5) foo)
  431.           (list)
  432.                => nil
  433.  
  434.  - Function: make-list LENGTH OBJECT
  435.      This function creates a list of length LENGTH, in which all the
  436.      elements have the identical value OBJECT.  Compare `make-list'
  437.      with `make-string' (*note Creating Strings::.).
  438.  
  439.           (make-list 3 'pigs)
  440.                => (pigs pigs pigs)
  441.           (make-list 0 'pigs)
  442.                => nil
  443.  
  444.  - Function: append &rest SEQUENCES
  445.      This function returns a list containing all the elements of
  446.      SEQUENCES.  The SEQUENCES may be lists, vectors, strings, or
  447.      integers.  All arguments except the last one are copied, so none
  448.      of them are altered.
  449.  
  450.      The final argument to `append' may be any object but it is
  451.      typically a list.  The final argument is not copied or converted;
  452.      it becomes part of the structure of the new list.
  453.  
  454.      Here is an example:
  455.  
  456.           (setq trees '(pine oak))
  457.                => (pine oak)
  458.           (setq more-trees (append '(maple birch) trees))
  459.                => (maple birch pine oak)
  460.           
  461.           trees
  462.                => (pine oak)
  463.           more-trees
  464.                => (maple birch pine oak)
  465.           (eq trees (cdr (cdr more-trees)))
  466.                => t
  467.  
  468.      You can see what happens by looking at a box diagram.  The variable
  469.      `trees' is set to the list `(pine oak)' and then the variable
  470.      `more-trees' is set to the list `(maple birch pine oak)'.
  471.      However, the variable `trees' continues to refer to the original
  472.      list:
  473.  
  474.           more-trees                trees
  475.           |                           |
  476.           |     ___ ___      ___ ___   -> ___ ___      ___ ___
  477.            --> |___|___|--> |___|___|--> |___|___|--> |___|___|--> nil
  478.                  |            |            |            |
  479.                  |            |            |            |
  480.                   --> maple    -->birch     --> pine     --> oak
  481.  
  482.      An empty sequence contributes nothing to the value returned by
  483.      `append'.  As a consequence of this, a final `nil' argument forces
  484.      a copy of the previous argument.
  485.  
  486.           trees
  487.                => (pine oak)
  488.           (setq wood (append trees ()))
  489.                => (pine oak)
  490.           wood
  491.                => (pine oak)
  492.           (eq wood trees)
  493.                => nil
  494.  
  495.      This once was the standard way to copy a list, before the function
  496.      `copy-sequence' was invented.  *Note Sequences Arrays Vectors::.
  497.  
  498.      With the help of `apply', we can append all the lists in a list of
  499.      lists:
  500.  
  501.           (apply 'append '((a b c) nil (x y z) nil))
  502.                => (a b c x y z)
  503.  
  504.      If no SEQUENCES are given, `nil' is returned:
  505.  
  506.           (append)
  507.                => nil
  508.  
  509.      In the special case where one of the SEQUENCES is an integer (not
  510.      a sequence of integers), it is first converted to a string of
  511.      digits making up the decimal print representation of the integer.
  512.      This special case exists for compatibility with Mocklisp, and we
  513.      don't recommend you take advantage of it.  If you want to convert
  514.      an integer in this way, use `format' (*note Formatting Strings::.)
  515.      or `number-to-string' (*note String Conversion::.).
  516.  
  517.           (setq trees '(pine oak))
  518.                => (pine oak)
  519.           (char-to-string 54)
  520.                => "6"
  521.           (setq longer-list (append trees 6 '(spruce)))
  522.                => (pine oak 54 spruce)
  523.           (setq x-list (append trees 6 6))
  524.                => (pine oak 54 . 6)
  525.  
  526.      See `nconc' in *Note Rearrangement::, for another way to join lists
  527.      without copying.
  528.  
  529.  - Function: reverse LIST
  530.      This function creates a new list whose elements are the elements of
  531.      LIST, but in reverse order.  The original argument LIST is *not*
  532.      altered.
  533.  
  534.           (setq x '(1 2 3 4))
  535.                => (1 2 3 4)
  536.           (reverse x)
  537.                => (4 3 2 1)
  538.           x
  539.                => (1 2 3 4)
  540.  
  541. 
  542. File: elisp,  Node: Modifying Lists,  Next: Sets And Lists,  Prev: Building Lists,  Up: Lists
  543.  
  544. Modifying Existing List Structure
  545. =================================
  546.  
  547.    You can modify the CAR and CDR contents of a cons cell with the
  548. primitives `setcar' and `setcdr'.
  549.  
  550.      Common Lisp note: Common Lisp uses functions `rplaca' and `rplacd'
  551.      to alter list structure; they change structure the same way as
  552.      `setcar' and `setcdr', but the Common Lisp functions return the
  553.      cons cell while `setcar' and `setcdr' return the new CAR or CDR.
  554.  
  555. * Menu:
  556.  
  557. * Setcar::          Replacing an element in a list.
  558. * Setcdr::          Replacing part of the list backbone.
  559.                       This can be used to remove or add elements.
  560. * Rearrangement::   Reordering the elements in a list; combining lists.
  561.  
  562. 
  563. File: elisp,  Node: Setcar,  Next: Setcdr,  Prev: Modifying Lists,  Up: Modifying Lists
  564.  
  565. Altering List Elements with `setcar'
  566. ------------------------------------
  567.  
  568.    Changing the CAR of a cons cell is done with `setcar' and replaces
  569. one element of a list with a different element.
  570.  
  571.  - Function: setcar CONS OBJECT
  572.      This function stores OBJECT as the new CAR of CONS, replacing its
  573.      previous CAR.  It returns the value OBJECT.  For example:
  574.  
  575.           (setq x '(1 2))
  576.                => (1 2)
  577.           (setcar x '4)
  578.                => 4
  579.           x
  580.                => (4 2)
  581.  
  582.    When a cons cell is part of the shared structure of several lists,
  583. storing a new CAR into the cons changes one element of each of these
  584. lists.  Here is an example:
  585.  
  586.      ;; Create two lists that are partly shared.
  587.      (setq x1 '(a b c))
  588.           => (a b c)
  589.      (setq x2 (cons 'z (cdr x1)))
  590.           => (z b c)
  591.      
  592.      ;; Replace the CAR of a shared link.
  593.      (setcar (cdr x1) 'foo)
  594.           => foo
  595.      x1                           ; Both lists are changed.
  596.           => (a foo c)
  597.      x2
  598.           => (z foo c)
  599.      
  600.      ;; Replace the CAR of a link that is not shared.
  601.      (setcar x1 'baz)
  602.           => baz
  603.      x1                           ; Only one list is changed.
  604.           => (baz foo c)
  605.      x2
  606.           => (z foo c)
  607.  
  608.    Here is a graphical depiction of the shared structure of the two
  609. lists X1 and X2, showing why replacing `b' changes them both:
  610.  
  611.              ___ ___        ___ ___      ___ ___
  612.      x1---> |___|___|----> |___|___|--> |___|___|--> nil
  613.               |        -->   |            |
  614.               |       |      |            |
  615.                --> a  |       --> b        --> c
  616.                       |
  617.             ___ ___   |
  618.      x2--> |___|___|--
  619.              |
  620.              |
  621.               --> z
  622.  
  623.    Here is an alternative form of box diagram, showing the same
  624. relationship:
  625.  
  626.      x1:
  627.       --------------       --------------       --------------
  628.      | car   | cdr  |     | car   | cdr  |     | car   | cdr  |
  629.      |   a   |   o------->|   b   |   o------->|   c   |  nil |
  630.      |       |      |  -->|       |      |     |       |      |
  631.       --------------  |    --------------       --------------
  632.                       |
  633.      x2:              |
  634.       --------------  |
  635.      | car   | cdr  | |
  636.      |   z   |   o----
  637.      |       |      |
  638.       --------------
  639.  
  640. 
  641. File: elisp,  Node: Setcdr,  Next: Rearrangement,  Prev: Setcar,  Up: Modifying Lists
  642.  
  643. Altering the CDR of a List
  644. --------------------------
  645.  
  646.    The lowest-level primitive for modifying a CDR is `setcdr':
  647.  
  648.  - Function: setcdr CONS OBJECT
  649.      This function stores OBJECT into the cdr of CONS.  The value
  650.      returned is OBJECT, not CONS.
  651.  
  652.    Here is an example of replacing the CDR of a list with a different
  653. list.  All but the first element of the list are removed in favor of a
  654. different sequence of elements.  The first element is unchanged,
  655. because it resides in the CAR of the list, and is not reached via the
  656. CDR.
  657.  
  658.      (setq x '(1 2 3))
  659.           => (1 2 3)
  660.      (setcdr x '(4))
  661.           => (4)
  662.      x
  663.           => (1 4)
  664.  
  665.    You can delete elements from the middle of a list by altering the
  666. CDRs of the cons cells in the list.  For example, here we delete the
  667. second element, `b', from the list `(a b c)', by changing the CDR of
  668. the first cell:
  669.  
  670.      (setq x1 '(a b c))
  671.           => (a b c)
  672.      (setcdr x1 (cdr (cdr x1)))
  673.           => (c)
  674.      x1
  675.           => (a c)
  676.  
  677.    Here is the result in box notation:
  678.  
  679.                         --------------------
  680.                        |                    |
  681.       --------------   |   --------------   |    --------------
  682.      | car   | cdr  |  |  | car   | cdr  |   -->| car   | cdr  |
  683.      |   a   |   o-----   |   b   |   o-------->|   c   |  nil |
  684.      |       |      |     |       |      |      |       |      |
  685.       --------------       --------------        --------------
  686.  
  687. The second cons cell, which previously held the element `b', still
  688. exists and its CAR is still `b', but it no longer forms part of this
  689. list.
  690.  
  691.    It is equally easy to insert a new element by changing CDRs:
  692.  
  693.      (setq x1 '(a b c))
  694.           => (a b c)
  695.      (setcdr x1 (cons 'd (cdr x1)))
  696.           => (d b c)
  697.      x1
  698.           => (a d b c)
  699.  
  700.    Here is this result in box notation:
  701.  
  702.      --------------        -------------       -------------
  703.      | car  | cdr   |      | car  | cdr  |     | car  | cdr  |
  704.      |   a  |   o   |   -->|   b  |   o------->|   c  |  nil |
  705.      |      |   |   |  |   |      |      |     |      |      |
  706.       --------- | --   |    -------------       -------------
  707.                 |      |
  708.           -----         --------
  709.          |                      |
  710.          |    ---------------   |
  711.          |   | car   | cdr   |  |
  712.           -->|   d   |   o------
  713.              |       |       |
  714.               ---------------
  715.  
  716. 
  717. File: elisp,  Node: Rearrangement,  Prev: Setcdr,  Up: Modifying Lists
  718.  
  719. Functions that Rearrange Lists
  720. ------------------------------
  721.  
  722.    Here are some functions that rearrange lists "destructively" by
  723. modifying the CDRs of their component cons cells.  We call these
  724. functions "destructive" because the original lists passed as arguments
  725. to them are chewed up to produce a new list that is subsequently
  726. returned.
  727.  
  728.  - Function: nconc &rest LISTS
  729.      This function returns a list containing all the elements of LISTS.
  730.      Unlike `append' (*note Building Lists::.), the LISTS are *not*
  731.      copied.  Instead, the last CDR of each of the LISTS is changed to
  732.      refer to the following list.  The last of the LISTS is not
  733.      altered.  For example:
  734.  
  735.           (setq x '(1 2 3))
  736.                => (1 2 3)
  737.           (nconc x '(4 5))
  738.                => (1 2 3 4 5)
  739.           x
  740.                => (1 2 3 4 5)
  741.  
  742.      Since the last argument of `nconc' is not itself modified, it is
  743.      reasonable to use a constant list, such as `'(4 5)', as is done in
  744.      the above example.  For the same reason, the last argument need
  745.      not be a list:
  746.  
  747.           (setq x '(1 2 3))
  748.                => (1 2 3)
  749.           (nconc x 'z)
  750.                => (1 2 3 . z)
  751.           x
  752.                => (1 2 3 . z)
  753.  
  754.      A common pitfall is to use a quoted constant list as a non-last
  755.      argument to `nconc'.  If you do this, your program will change
  756.      each time you run it!  Here is what happens:
  757.  
  758.           (defun add-foo (x)            ; This function should add
  759.             (nconc '(foo) x))           ;   `foo' to the front of its arg.
  760.  
  761.           (symbol-function 'add-foo)
  762.                => (lambda (x) (nconc (quote (foo)) x))
  763.  
  764.           (setq xx (add-foo '(1 2)))    ; It seems to work.
  765.                => (foo 1 2)
  766.  
  767.           (setq xy (add-foo '(3 4)))    ; What happened?
  768.                => (foo 1 2 3 4)
  769.  
  770.           (eq xx xy)
  771.                => t
  772.  
  773.           (symbol-function 'add-foo)
  774.                => (lambda (x) (nconc (quote (foo 1 2 3 4) x)))
  775.  
  776.  - Function: nreverse LIST
  777.      This function reverses the order of the elements of LIST.  Unlike
  778.      `reverse', `nreverse' alters its argument destructively by
  779.      reversing the CDRs in the cons cells forming the list.  The cons
  780.      cell which used to be the last one in LIST becomes the first cell
  781.      of the value.
  782.  
  783.      For example:
  784.  
  785.           (setq x '(1 2 3 4))
  786.                => (1 2 3 4)
  787.           x
  788.                => (1 2 3 4)
  789.           (nreverse x)
  790.                => (4 3 2 1)
  791.           ;; The cell that was first is now last.
  792.           x
  793.                => (1)
  794.  
  795.      To avoid confusion, we usually store the result of `nreverse' back
  796.      in the same variable which held the original list:
  797.  
  798.           (setq x (nreverse x))
  799.  
  800.      Here is the `nreverse' of our favorite example, `(a b c)',
  801.      presented graphically:
  802.  
  803.           Original list head:                       Reversed list:
  804.            -------------        -------------        ------------
  805.           | car  | cdr  |      | car  | cdr  |      | car | cdr  |
  806.           |   a  |  nil |<--   |   b  |   o  |<--   |   c |   o  |
  807.           |      |      |   |  |      |   |  |   |  |     |   |  |
  808.            -------------    |   --------- | -    |   -------- | -
  809.                             |             |      |            |
  810.                              -------------        ------------
  811.  
  812.  - Function: sort LIST PREDICATE
  813.      This function sorts LIST stably, though destructively, and returns
  814.      the sorted list.  It compares elements using PREDICATE.  A stable
  815.      sort is one in which elements with equal sort keys maintain their
  816.      relative order before and after the sort.  Stability is important
  817.      when successive sorts are used to order elements according to
  818.      different criteria.
  819.  
  820.      The argument PREDICATE must be a function that accepts two
  821.      arguments.  It is called with two elements of LIST.  To get an
  822.      increasing order sort, the PREDICATE should return `t' if the
  823.      first element is "less than" the second, or `nil' if not.
  824.  
  825.      The destructive aspect of `sort' is that it rearranges the cons
  826.      cells forming LIST by changing CDRs.  A nondestructive sort
  827.      function would create new cons cells to store the elements in their
  828.      sorted order.  If you wish to sort a list without destroying the
  829.      original, copy it first with `copy-sequence'.
  830.  
  831.      The CARs of the cons cells are not changed; the cons cell that
  832.      originally contained the element `a' in LIST still has `a' in its
  833.      CAR after sorting, but it now appears in a different position in
  834.      the list due to the change of CDRs.  For example:
  835.  
  836.           (setq nums '(1 3 2 6 5 4 0))
  837.                => (1 3 2 6 5 4 0)
  838.           (sort nums '<)
  839.                => (0 1 2 3 4 5 6)
  840.           nums
  841.                => (1 2 3 4 5 6)
  842.  
  843.      Note that the list in `nums' no longer contains 0; this is the same
  844.      cons cell that it was before, but it is no longer the first one in
  845.      the list.  Don't assume a variable that formerly held the argument
  846.      now holds the entire sorted list!  Instead, save the result of
  847.      `sort' and use that.  Most often we store the result back into the
  848.      variable that held the original list:
  849.  
  850.           (setq nums (sort nums '<))
  851.  
  852.      *Note Sorting::, for more functions that perform sorting.  See
  853.      `documentation' in *Note Accessing Documentation::, for a useful
  854.      example of `sort'.
  855.  
  856.    See `delq', in *Note Sets And Lists::, for another function that
  857. modifies cons cells.
  858.  
  859. 
  860. File: elisp,  Node: Sets And Lists,  Next: Association Lists,  Prev: Modifying Lists,  Up: Lists
  861.  
  862. Using Lists as Sets
  863. ===================
  864.  
  865.    A list can represent an unordered mathematical set--simply consider a
  866. value an element of a set if it appears in the list, and ignore the
  867. order of the list.  To form the union of two sets, use `append' (as
  868. long as you don't mind having duplicate elements).  Other useful
  869. functions for sets include `memq' and `delq', and their `equal'
  870. versions, `member' and `delete'.
  871.  
  872.      Common Lisp note: Common Lisp has functions `union' (which avoids
  873.      duplicate elements) and `intersection' for set operations, but GNU
  874.      Emacs Lisp does not have them.  You can write them in Lisp if you
  875.      wish.
  876.  
  877.  - Function: memq OBJECT LIST
  878.      This function tests to see whether OBJECT is a member of LIST.  If
  879.      it is, `memq' returns a list starting with the first occurrence of
  880.      OBJECT.  Otherwise, it returns `nil'.  The letter `q' in `memq'
  881.      says that it uses `eq' to compare OBJECT against the elements of
  882.      the list.  For example:
  883.  
  884.           (memq 2 '(1 2 3 2 1))
  885.                => (2 3 2 1)
  886.           (memq '(2) '((1) (2)))    ; `(2)' and `(2)' are not `eq'.
  887.                => nil
  888.  
  889.  - Function: delq OBJECT LIST
  890.      This function removes all elements `eq' to OBJECT from LIST.  The
  891.      letter `q' in `delq' says that it uses `eq' to compare OBJECT
  892.      against the elements of the list, like `memq'.
  893.  
  894.    When `delq' deletes elements from the front of the list, it does so
  895. simply by advancing down the list and returning a sublist that starts
  896. after those elements:
  897.  
  898.      (delq 'a '(a b c))
  899.      ==
  900.      (cdr '(a b c))
  901.  
  902.    When an element to be deleted appears in the middle of the list,
  903. removing it involves changing the CDRs (*note Setcdr::.).
  904.  
  905.      (setq sample-list '(1 2 3 (4)))
  906.           => (1 2 3 (4))
  907.      (delq 1 sample-list)
  908.           => (2 3 (4))
  909.      sample-list
  910.           => (1 2 3 (4))
  911.      (delq 2 sample-list)
  912.           => (1 3 (4))
  913.      sample-list
  914.           => (1 3 (4))
  915.  
  916.    Note that `(delq 2 sample-list)' modifies `sample-list' to splice
  917. out the second element, but `(delq 1 sample-list)' does not splice
  918. anything--it just returns a shorter list.  Don't assume that a variable
  919. which formerly held the argument LIST now has fewer elements, or that
  920. it still holds the original list!  Instead, save the result of `delq'
  921. and use that.  Most often we store the result back into the variable
  922. that held the original list:
  923.  
  924.      (setq flowers (delq 'rose flowers))
  925.  
  926.    In the following example, the `(4)' that `delq' attempts to match
  927. and the `(4)' in the `sample-list' are not `eq':
  928.  
  929.      (delq '(4) sample-list)
  930.           => (1 3 (4))
  931.  
  932.    The following two functions are like `memq' and `delq' but use
  933. `equal' rather than `eq' to compare elements.  They are new in Emacs 19.
  934.  
  935.  - Function: member OBJECT LIST
  936.      The function `member' tests to see whether OBJECT is a member of
  937.      LIST, comparing members with OBJECT using `equal'.  If OBJECT is a
  938.      member, `memq' returns a list starting with its first occurrence
  939.      in LIST.  Otherwise, it returns `nil'.
  940.  
  941.      Compare this with `memq':
  942.  
  943.           (member '(2) '((1) (2)))  ; `(2)' and `(2)' are `equal'.
  944.                => ((2))
  945.           (memq '(2) '((1) (2)))    ; `(2)' and `(2)' are not `eq'.
  946.                => nil
  947.           ;; Two strings with the same contents are `equal'.
  948.           (member "foo" '("foo" "bar"))
  949.                => ("foo" "bar")
  950.  
  951.  - Function: delete OBJECT LIST
  952.      This function removes all elements `equal' to OBJECT from LIST.
  953.      It is to `delq' as `member' is to `memq': it uses `equal' to
  954.      compare elements with OBJECT, like `member'; when it finds an
  955.      element that matches, it removes the element just as `delq' would.
  956.      For example:
  957.  
  958.           (delete '(2) '((2) (1) (2)))
  959.                => '((1))
  960.  
  961.      Common Lisp note: The functions `member' and `delete' in GNU Emacs
  962.      Lisp are derived from Maclisp, not Common Lisp.  The Common Lisp
  963.      versions do not use `equal' to compare elements.
  964.  
  965. 
  966. File: elisp,  Node: Association Lists,  Prev: Sets And Lists,  Up: Lists
  967.  
  968. Association Lists
  969. =================
  970.  
  971.    An "association list", or "alist" for short, records a mapping from
  972. keys to values.  It is a list of cons cells called "associations": the
  973. CAR of each cell is the "key", and the CDR is the "associated value".
  974. (This usage of "key" is not related to the term "key sequence"; it
  975. means any object which can be looked up in a table.)
  976.  
  977.    Here is an example of an alist.  The key `pine' is associated with
  978. the value `cones'; the key `oak' is associated with `acorns'; and the
  979. key `maple' is associated with `seeds'.
  980.  
  981.      '((pine . cones)
  982.        (oak . acorns)
  983.        (maple . seeds))
  984.  
  985.    The associated values in an alist may be any Lisp objects; so may the
  986. keys.  For example, in the following alist, the symbol `a' is
  987. associated with the number `1', and the string `"b"' is associated with
  988. the *list* `(2 3)', which is the CDR of the alist element:
  989.  
  990.      ((a . 1) ("b" 2 3))
  991.  
  992.    Sometimes it is better to design an alist to store the associated
  993. value in the CAR of the CDR of the element.  Here is an example:
  994.  
  995.      '((rose red) (lily white) (buttercup yellow)))
  996.  
  997. Here we regard `red' as the value associated with `rose'.  One
  998. advantage of this method is that you can store other related
  999. information--even a list of other items--in the CDR of the CDR.  One
  1000. disadvantage is that you cannot use `rassq' (see below) to find the
  1001. element containing a given value.  When neither of these considerations
  1002. is important, the choice is a matter of taste, as long as you are
  1003. consistent about it for any given alist.
  1004.  
  1005.    Note that the same alist shown above could be regarded as having the
  1006. associated value in the CDR of the element; the value associated with
  1007. `rose' would be the list `(red)'.
  1008.  
  1009.    Association lists are often used to record information that you might
  1010. otherwise keep on a stack, since new associations may be added easily to
  1011. the front of the list.  When searching an association list for an
  1012. association with a given key, the first one found is returned, if there
  1013. is more than one.
  1014.  
  1015.    In Emacs Lisp, it is *not* an error if an element of an association
  1016. list is not a cons cell.  The alist search functions simply ignore such
  1017. elements.  Many other versions of Lisp signal errors in such cases.
  1018.  
  1019.    Note that property lists are similar to association lists in several
  1020. respects.  A property list behaves like an association list in which
  1021. each key can occur only once.  *Note Property Lists::, for a comparison
  1022. of property lists and association lists.
  1023.  
  1024.  - Function: assoc KEY ALIST
  1025.      This function returns the first association for KEY in ALIST.  It
  1026.      compares KEY against the alist elements using `equal' (*note
  1027.      Equality Predicates::.).  It returns `nil' if no association in
  1028.      ALIST has a CAR `equal' to KEY.  For example:
  1029.  
  1030.           (setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
  1031.                => ((pine . cones) (oak . acorns) (maple . seeds))
  1032.           (assoc 'oak trees)
  1033.                => (oak . acorns)
  1034.           (cdr (assoc 'oak trees))
  1035.                => acorns
  1036.           (assoc 'birch trees)
  1037.                => nil
  1038.  
  1039.      Here is another example in which the keys and values are not
  1040.      symbols:
  1041.  
  1042.           (setq needles-per-cluster
  1043.                 '((2 . ("Austrian Pine" "Red Pine"))
  1044.                   (3 . "Pitch Pine")
  1045.                   (5 . "White Pine")))
  1046.           
  1047.           (cdr (assoc 3 needles-per-cluster))
  1048.                => "Pitch Pine"
  1049.           (cdr (assoc 2 needles-per-cluster))
  1050.                => ("Austrian Pine" "Red Pine")
  1051.  
  1052.  - Function: assq KEY ALIST
  1053.      This function is like `assoc' in that it returns the first
  1054.      association for KEY in ALIST, but it makes the comparison using
  1055.      `eq' instead of `equal'.  `assq' returns `nil' if no association
  1056.      in ALIST has a CAR `eq' to KEY.  This function is used more often
  1057.      than `assoc', since `eq' is faster than `equal' and most alists
  1058.      use symbols as keys.  *Note Equality Predicates::.
  1059.  
  1060.           (setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
  1061.           
  1062.           (assq 'pine trees)
  1063.                => (pine . cones)
  1064.  
  1065.      On the other hand, `assq' is not usually useful in alists where the
  1066.      keys may not be symbols:
  1067.  
  1068.           (setq leaves
  1069.                 '(("simple leaves" . oak)
  1070.                   ("compound leaves" . horsechestnut)))
  1071.           
  1072.           (assq "simple leaves" leaves)
  1073.                => nil
  1074.           (assoc "simple leaves" leaves)
  1075.                => ("simple leaves" . oak)
  1076.  
  1077.  - Function: rassq VALUE ALIST
  1078.      This function returns the first association with value VALUE in
  1079.      ALIST.  It returns `nil' if no association in ALIST has a CDR `eq'
  1080.      to VALUE.
  1081.  
  1082.      `rassq' is like `assq' except that the CDR of the ALIST
  1083.      associations is tested instead of the CAR.  You can think of this
  1084.      as "reverse `assq'", finding the key for a given value.
  1085.  
  1086.      For example:
  1087.  
  1088.           (setq trees '((pine . cones) (oak . acorns) (maple . seeds)))
  1089.           
  1090.           (rassq 'acorns trees)
  1091.                => (oak . acorns)
  1092.           (rassq 'spores trees)
  1093.                => nil
  1094.  
  1095.      Note that `rassq' cannot be used to search for a value stored in
  1096.      the CAR of the CDR of an element:
  1097.  
  1098.           (setq colors '((rose red) (lily white) (buttercup yellow)))
  1099.           
  1100.           (rassq 'white colors)
  1101.                => nil
  1102.  
  1103.      In this case, the CDR of the association `(lily white)' is not the
  1104.      symbol `white', but rather the list `(white)'.  This can be seen
  1105.      more clearly if the association is written in dotted pair notation:
  1106.  
  1107.           (lily white) == (lily . (white))
  1108.  
  1109.  - Function: copy-alist ALIST
  1110.      This function returns a two-level deep copy of ALIST: it creates a
  1111.      new copy of each association, so that you can alter the
  1112.      associations of the new alist without changing the old one.
  1113.  
  1114.           (setq needles-per-cluster
  1115.                 '((2 . ("Austrian Pine" "Red Pine"))
  1116.                   (3 . "Pitch Pine")
  1117.                   (5 . "White Pine")))
  1118.           =>
  1119.           ((2 "Austrian Pine" "Red Pine")
  1120.            (3 . "Pitch Pine")
  1121.            (5 . "White Pine"))
  1122.           
  1123.           (setq copy (copy-alist needles-per-cluster))
  1124.           =>
  1125.           ((2 "Austrian Pine" "Red Pine")
  1126.            (3 . "Pitch Pine")
  1127.            (5 . "White Pine"))
  1128.           
  1129.           (eq needles-per-cluster copy)
  1130.                => nil
  1131.           (equal needles-per-cluster copy)
  1132.                => t
  1133.           (eq (car needles-per-cluster) (car copy))
  1134.                => nil
  1135.           (cdr (car (cdr needles-per-cluster)))
  1136.                => "Pitch Pine"
  1137.           (eq (cdr (car (cdr needles-per-cluster)))
  1138.               (cdr (car (cdr copy))))
  1139.                => t
  1140.  
  1141. 
  1142. File: elisp,  Node: Sequences Arrays Vectors,  Next: Symbols,  Prev: Lists,  Up: Top
  1143.  
  1144. Sequences, Arrays, and Vectors
  1145. ******************************
  1146.  
  1147.    Recall that the "sequence" type is the union of three other Lisp
  1148. types: lists, vectors, and strings.  In other words, any list is a
  1149. sequence, any vector is a sequence, and any string is a sequence.  The
  1150. common property that all sequences have is that each is an ordered
  1151. collection of elements.
  1152.  
  1153.    An "array" is a single primitive object directly containing all its
  1154. elements.  Therefore, all the elements are accessible in constant time.
  1155. The length of an existing array cannot be changed.  Both strings and
  1156. vectors are arrays.  A list is a sequence of elements, but it is not a
  1157. single primitive object; it is made of cons cells, one cell per
  1158. element.  Therefore, elements farther from the beginning of the list
  1159. take longer to access, but it is possible to add elements to the list or
  1160. remove elements.  The elements of vectors and lists may be any Lisp
  1161. objects.  The elements of strings are all characters.
  1162.  
  1163.    The following diagram shows the relationship between these types:
  1164.  
  1165.                  ___________________________________
  1166.                 |                                   |
  1167.                 |          Sequence                 |
  1168.                 |  ______   ______________________  |
  1169.                 | |      | |                      | |
  1170.                 | | List | |         Array        | |
  1171.                 | |      | |  ________   _______  | |
  1172.                 | |______| | |        | |       | | |
  1173.                 |          | | String | | Vector| | |
  1174.                 |          | |________| |_______| | |
  1175.                 |          |______________________| |
  1176.                 |___________________________________|
  1177.  
  1178.           The Relationship between Sequences, Arrays, and Vectors
  1179.  
  1180.  
  1181. * Menu:
  1182.  
  1183. * Sequence Functions::    Functions that accept any kind of sequence.
  1184. * Arrays::                Characteristics of arrays in Emacs Lisp.
  1185. * Array Functions::       Functions specifically for arrays.
  1186. * Vectors::               Functions specifically for vectors.
  1187.  
  1188. 
  1189. File: elisp,  Node: Sequence Functions,  Next: Arrays,  Prev: Sequences Arrays Vectors,  Up: Sequences Arrays Vectors
  1190.  
  1191. Sequences
  1192. =========
  1193.  
  1194.    In Emacs Lisp, a "sequence" is either a list, a vector or a string.
  1195. The common property that all sequences have is that each is an ordered
  1196. collection of elements.  This section describes functions that accept
  1197. any kind of sequence.
  1198.  
  1199.  - Function: sequencep OBJECT
  1200.      Returns `t' if OBJECT is a list, vector, or string, `nil'
  1201.      otherwise.
  1202.  
  1203.  - Function: copy-sequence SEQUENCE
  1204.      Returns a copy of SEQUENCE.  The copy is the same type of object
  1205.      as the original sequence, and it has the same elements in the same
  1206.      order.
  1207.  
  1208.      Storing a new element into the copy does not affect the original
  1209.      SEQUENCE, and vice versa.  However, the elements of the new
  1210.      sequence are not copies; they are identical (`eq') to the elements
  1211.      of the original.  Therefore, changes made within these elements, as
  1212.      found via the copied sequence, are also visible in the original
  1213.      sequence.
  1214.  
  1215.      If the sequence is a string with text properties, the property
  1216.      list in the copy is itself a copy, not shared with the original's
  1217.      property list.  However, the actual values of the properties are
  1218.      shared.  *Note Text Properties::.
  1219.  
  1220.      See also `append' in *Note Building Lists::, `concat' in *Note
  1221.      Creating Strings::, and `vconcat' in *Note Vectors::, for others
  1222.      ways to copy sequences.
  1223.  
  1224.           (setq bar '(1 2))
  1225.                => (1 2)
  1226.           (setq x (vector 'foo bar))
  1227.                => [foo (1 2)]
  1228.           (setq y (copy-sequence x))
  1229.                => [foo (1 2)]
  1230.           
  1231.           (eq x y)
  1232.                => nil
  1233.           (equal x y)
  1234.                => t
  1235.           (eq (elt x 1) (elt y 1))
  1236.                => t
  1237.           
  1238.           ;; Replacing an element of one sequence.
  1239.           (aset x 0 'quux)
  1240.           x => [quux (1 2)]
  1241.           y => [foo (1 2)]
  1242.           
  1243.           ;; Modifying the inside of a shared element.
  1244.           (setcar (aref x 1) 69)
  1245.           x => [quux (69 2)]
  1246.           y => [foo (69 2)]
  1247.  
  1248.  - Function: length SEQUENCE
  1249.      Returns the number of elements in SEQUENCE.  If SEQUENCE is a cons
  1250.      cell that is not a list (because the final CDR is not `nil'), a
  1251.      `wrong-type-argument' error is signaled.
  1252.  
  1253.           (length '(1 2 3))
  1254.               => 3
  1255.           (length ())
  1256.               => 0
  1257.           (length "foobar")
  1258.               => 6
  1259.           (length [1 2 3])
  1260.               => 3
  1261.  
  1262.  - Function: elt SEQUENCE INDEX
  1263.      This function returns the element of SEQUENCE indexed by INDEX.
  1264.      Legitimate values of INDEX are integers ranging from 0 up to one
  1265.      less than the length of SEQUENCE.  If SEQUENCE is a list, then
  1266.      out-of-range values of index return `nil'; otherwise, they produce
  1267.      an `args-out-of-range' error.
  1268.  
  1269.           (elt [1 2 3 4] 2)
  1270.                => 3
  1271.           (elt '(1 2 3 4) 2)
  1272.                => 3
  1273.           (char-to-string (elt "1234" 2))
  1274.                => "3"
  1275.           (elt [1 2 3 4] 4)
  1276.                error-->Args out of range: [1 2 3 4], 4
  1277.           (elt [1 2 3 4] -1)
  1278.                error-->Args out of range: [1 2 3 4], -1
  1279.  
  1280.      This function duplicates `aref' (*note Array Functions::.) and
  1281.      `nth' (*note List Elements::.), except that it works for any kind
  1282.      of sequence.
  1283.  
  1284. 
  1285. File: elisp,  Node: Arrays,  Next: Array Functions,  Prev: Sequence Functions,  Up: Sequences Arrays Vectors
  1286.  
  1287. Arrays
  1288. ======
  1289.  
  1290.    An "array" object refers directly to a number of other Lisp objects,
  1291. called the elements of the array.  Any element of an array may be
  1292. accessed in constant time.  In contrast, an element of a list requires
  1293. access time that is proportional to the position of the element in the
  1294. list.
  1295.  
  1296.    When you create an array, you must specify how many elements it has.
  1297. The amount of space allocated depends on the number of elements.
  1298. Therefore, it is impossible to change the size of an array once it is
  1299. created.  You cannot add or remove elements.  However, you can replace
  1300. an element with a different value.
  1301.  
  1302.    Emacs defines two types of array, both of which are one-dimensional:
  1303. "strings" and "vectors".  A vector is a general array; its elements can
  1304. be any Lisp objects.  A string is a specialized array; its elements
  1305. must be characters (i.e., integers between 0 and 255).  Each type of
  1306. array has its own read syntax.  *Note String Type::, and *Note Vector
  1307. Type::.
  1308.  
  1309.    Both kinds of arrays share these characteristics:
  1310.  
  1311.    * The first element of an array has index zero, the second element
  1312.      has index 1, and so on.  This is called "zero-origin" indexing.
  1313.      For example, an array of four elements has indices 0, 1, 2, and 3.
  1314.  
  1315.    * The elements of an array may be referenced or changed with the
  1316.      functions `aref' and `aset', respectively (*note Array
  1317.      Functions::.).
  1318.  
  1319.    In principle, if you wish to have an array of characters, you could
  1320. use either a string or a vector.  In practice, we always choose strings
  1321. for such applications, for four reasons:
  1322.  
  1323.    * They occupy one-fourth the space of a vector of the same elements.
  1324.  
  1325.    * Strings are printed in a way that shows the contents more clearly
  1326.      as characters.
  1327.  
  1328.    * Strings can hold text properties.  *Note Text Properties::.
  1329.  
  1330.    * Many of the specialized editing and I/O facilities of Emacs accept
  1331.      only strings.  For example, you cannot insert a vector of
  1332.      characters into a buffer the way you can insert a string.  *Note
  1333.      Strings and Characters::.
  1334.  
  1335.